
package com.tools.cleanmaster.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.TransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import com.tools.ad.util.FontsManager;
import com.tools.ad.util.FontsManager;
import com.tools.cleanmaster.R;

import java.util.Locale;

/**
 * 修改内容为大写，但是对于Spanned无效，需要自行设置为大写。
 */
public class AllCapsButton extends Button {

    private int mFontType = -1;

    private TransformationMethod allCapsTransformer = new TransformationMethod() {
        @Override
        public void onFocusChanged(View view, CharSequence sourceText,
                boolean focused, int direction, Rect previouslyFocusedRect) {
        }

        @Override
        public CharSequence getTransformation(CharSequence source, View view) {
            if (!TextUtils.isEmpty(source)) {
                if (source instanceof Spanned) {
                    return source;
                } else {
                    return source.toString().toUpperCase(mLocale);
                }
            } else {
                return source;
            }
        }
    };

    private Locale mLocale;

    public AllCapsButton(Context context) {
        this(context, null);
    }

    public AllCapsButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mLocale = context.getResources().getConfiguration().locale;
        int type = getAttrFontType(context, attrs);
        setFontType(type);
        setTransformationMethod(allCapsTransformer);
    }

    public AllCapsButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mLocale = context.getResources().getConfiguration().locale;
        int type = getAttrFontType(context, attrs);
        setFontType(type);
        setTransformationMethod(allCapsTransformer);
    }

    @SuppressWarnings("static-access")
    static int getAttrFontType(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.FontTextView);
        int indexCount = a.getIndexCount();
        int type = FontsManager.FONT_LIGHT;
        for (int i = 0; i < indexCount; i++) {
            int index = a.getIndex(i);
            if (index == R.styleable.FontTextView_fontType) {
                type = a.getInteger(index, FontsManager.FONT_LIGHT);
                break;
            }
        }
        a.recycle();
        return type;
    }

    /**
     * 设置字体类别，THIN 用于大圈内部的数字，LIGHT 用于正文英文字体
     * 
     * @param  .THIN 或 FontTextView.LIGHT
     */
    public void setFontType(int type) {
        if (!FontsManager.isEnglish(getContext()) || type == mFontType) {
            return;
        }
        Typeface tf = FontsManager.getFontType(FontsManager.FONT_LIGHT);
        if (tf != null) {
            setTypeface(tf);
        }
    }

    /**
     * 获取字体类别
     * 
     * @return FontTextView.THIN 或 FontTextView.LIGHT
     */
    public int getFontType() {
        return mFontType;
    }
}
